package com.git.bds.mr.patient.service;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.git.bds.mr.page.PageParam;
import com.git.bds.mr.page.PageResult;
import com.git.bds.mr.patient.convert.PatientBizConvert;
import com.git.bds.mr.patient.mapper.PatientInfoMapper;
import com.git.bds.mr.patient.model.domain.PatientInfo;
import com.git.bds.mr.patient.model.dto.PatientDTO;
import com.git.bds.mr.records.mapper.AmendmentsMapper;
import com.git.bds.mr.records.mapper.RecordsMapper;
import com.git.bds.mr.records.model.domain.Amendments;
import com.git.bds.mr.records.model.domain.Records;
import com.git.bds.mr.records.model.dto.PatientRecordDTO;
import com.git.bds.mr.records.model.dto.RecordDTO;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户个人信息表 服务实现类
 * </p>
 *
 * @author 成大事
 * @since 2022-09-23 18:08:01
 */
@Service
public class PatientInfoServiceImpl extends MPJBaseServiceImpl<PatientInfoMapper, PatientInfo> implements PatientInfoService {

    @Resource
    private RecordsMapper recordsMapper;

    @Resource
    private AmendmentsMapper amendmentsMapper;

    /**
     * 更新自我信息
     *
     * @param patientDTO 患者dto
     * @return {@link Boolean}
     */
    @Override
    public Boolean updateSelfInfo(PatientDTO patientDTO) {
        PatientInfo patientInfo = PatientBizConvert.INSTANCE.toPatientInfo(patientDTO);
        return this.baseMapper.updateById(patientInfo) > 0;
    }

    /**
     * 按页获取记录
     *
     * @param pageParam 页面参数
     * @return {@link PageResult}<{@link RecordDTO}>
     */
    @Override
    public PageResult<PatientRecordDTO> getRecordsByPage(PageParam pageParam) {
        List<PatientRecordDTO> recordDTOList = recordsMapper.patientGetRecordsByPage(StpUtil.getLoginIdAsLong(), pageParam.getPageNo(), pageParam.getPageSize());
        return new PageResult<>(recordDTOList,(long) recordDTOList.size());
    }

    /**
     * 提议变更
     *
     * @param id     身份证件
     * @param remark 评论
     * @return {@link Boolean}
     */
    @Override
    public Boolean proposedChanges(Long id, String remark) {
        Long doctorId = recordsMapper.selectOne(new LambdaQueryWrapper<Records>()
                .select(Records::getDoctorId)
                .eq(Records::getId, id)).getDoctorId();
        long patientId = StpUtil.getLoginIdAsLong();
        Amendments amendments = PatientBizConvert.INSTANCE.toAmendments(doctorId, patientId, remark);
        return amendmentsMapper.insert(amendments) > 0;
    }
}
